// pages/order-detail/order-detail.js
Page({
	data: {
		orderId: '',
		patientName: '',
		prescriptionId: '',
		orderTime: '',
		deliveryType: 'self', // self: 自取, home: 配送
		deliveryTypeText: '',
		campus: '',
		medicineCost: '',
		totalCost: '',
		medicineList: [],
		statusIcon: '',
		statusText: '',
		statusDesc: '',
		deliveryStatus: '',
		estimatedDelivery: '',
		deliveryAddress: ''
	},

	onLoad(options) {
		console.log('订单详情页面加载')
		console.log('页面参数:', options)
		
		// 生成订单号
		const orderId = 'ORD' + Date.now().toString().slice(-8)
		
		// 获取当前时间
		const now = new Date()
		const orderTime = this.formatDateTime(now)
		
		const data = {
			orderId: orderId,
			orderTime: orderTime
		}
		
		if (options.patient) {
			data.patientName = options.patient
		}
		
		if (options.prescriptionId) {
			data.prescriptionId = options.prescriptionId
		}
		
		if (options.deliveryType) {
			data.deliveryType = options.deliveryType
			data.deliveryTypeText = options.deliveryType === 'self' ? '到院自取' : '送药上门'
		} else {
			data.deliveryTypeText = '到院自取'
		}
		
		if (options.campus) {
			data.campus = options.campus
		} else {
			data.campus = '湘南院区'
		}
		
		if (options.medicineCost) {
			data.medicineCost = options.medicineCost
		}
		
		if (options.totalCost) {
			data.totalCost = options.totalCost
		}
		
		// 加载订单详情
		this.loadOrderDetail(options.prescriptionId, data)
	},

	// 加载订单详情
	loadOrderDetail(prescriptionId, baseData) {
		// 根据处方ID获取订单详情
		const orderDetails = this.getOrderDetails(prescriptionId)
		
		// 合并基础数据和订单详情
		const data = {
			...baseData,
			...orderDetails
		}
		
		this.setData(data)
	},

	// 获取订单详情数据
	getOrderDetails(prescriptionId) {
		const orderData = {
			'001': {
				statusIcon: '✓',
				statusText: '配药完成',
				statusDesc: '药品已配好，请及时取药',
				deliveryStatus: '配药完成',
				estimatedDelivery: '工作日 8:00-17:30',
				medicineList: [
					{
						name: '阿莫西林胶囊',
						specification: '0.25g*24粒/盒',
						manufacturer: '华北制药股份有限公司',
						quantity: '2盒',
						usage: '口服',
						frequency: '每日3次',
						duration: '7天',
						price: 15.80
					},
					{
						name: '布洛芬缓释胶囊',
						specification: '0.3g*12粒/盒',
						manufacturer: '中美天津史克制药有限公司',
						quantity: '1盒',
						usage: '口服',
						frequency: '每日2次',
						duration: '3天',
						price: 12.50
					},
					{
						name: '复方甘草片',
						specification: '24片/盒',
						manufacturer: '哈药集团制药总厂',
						quantity: '2盒',
						usage: '含服',
						frequency: '每日3次',
						duration: '7天',
						price: 8.90
					}
				]
			},
			'002': {
				statusIcon: '⏳',
				statusText: '配药中',
				statusDesc: '药房正在为您配药，请耐心等待',
				deliveryStatus: '配药中',
				estimatedDelivery: '预计1-2小时内完成',
				medicineList: [
					{
						name: '头孢克肟分散片',
						specification: '0.1g*12片/盒',
						manufacturer: '齐鲁制药有限公司',
						quantity: '3盒',
						usage: '口服',
						frequency: '每日2次',
						duration: '10天',
						price: 28.60
					},
					{
						name: '氨溴特罗片',
						specification: '20片/盒',
						manufacturer: '勃林格殷格翰制药有限公司',
						quantity: '2盒',
						usage: '口服',
						frequency: '每日2次',
						duration: '10天',
						price: 45.80
					}
				]
			},
			'003': {
				statusIcon: '🚚',
				statusText: '配送中',
				statusDesc: '药品正在配送途中，请保持手机畅通',
				deliveryStatus: '配送中',
				estimatedDelivery: '预计今日下午送达',
				deliveryAddress: '湖南省长沙市岳麓区湘南院区附近',
				medicineList: [
					{
						name: '奥美拉唑肠溶胶囊',
						specification: '20mg*14粒/盒',
						manufacturer: '阿斯利康制药有限公司',
						quantity: '2盒',
						usage: '口服',
						frequency: '每日1次',
						duration: '14天',
						price: 35.20
					},
					{
						name: '枸橼酸铋钾胶囊',
						specification: '0.3g*24粒/盒',
						manufacturer: '丽珠医药集团股份有限公司',
						quantity: '2盒',
						usage: '口服',
						frequency: '每日3次',
						duration: '14天',
						price: 18.90
					}
				]
			},
			'005': {
				statusIcon: '✓',
				statusText: '配药完成',
				statusDesc: '药品已配好，请及时取药',
				deliveryStatus: '配药完成',
				estimatedDelivery: '工作日 8:00-17:30',
				medicineList: [
					{
						name: '硝苯地平缓释片',
						specification: '30mg*7片/盒',
						manufacturer: '拜耳医药保健有限公司',
						quantity: '4盒',
						usage: '口服',
						frequency: '每日1次',
						duration: '28天',
						price: 42.50
					},
					{
						name: '氢氯噻嗪片',
						specification: '25mg*100片/瓶',
						manufacturer: '华润双鹤药业股份有限公司',
						quantity: '1瓶',
						usage: '口服',
						frequency: '每日1次',
						duration: '30天',
						price: 15.80
					}
				]
			},
			'009': {
				statusIcon: '⏳',
				statusText: '配药中',
				statusDesc: '药房正在为您配药，请耐心等待',
				deliveryStatus: '配药中',
				estimatedDelivery: '预计1-2小时内完成',
				medicineList: [
					{
						name: '左甲状腺素钠片',
						specification: '50μg*30片/盒',
						manufacturer: '默克雪兰诺有限公司',
						quantity: '2盒',
						usage: '口服',
						frequency: '每日1次',
						duration: '30天',
						price: 25.60
					},
					{
						name: '硒酵母片',
						specification: '50μg*60片/盒',
						manufacturer: '北京同仁堂科技发展股份有限公司',
						quantity: '1盒',
						usage: '口服',
						frequency: '每日2次',
						duration: '30天',
						price: 38.90
					}
				]
			},
			'012': {
				statusIcon: '🚚',
				statusText: '配送中',
				statusDesc: '药品正在配送途中，请保持手机畅通',
				deliveryStatus: '配送中',
				estimatedDelivery: '预计今日下午送达',
				deliveryAddress: '湖南省长沙市岳麓区湘南院区附近',
				medicineList: [
					{
						name: '糠酸莫米松乳膏',
						specification: '10g/支',
						manufacturer: '拜耳医药保健有限公司',
						quantity: '2支',
						usage: '外用',
						frequency: '每日2次',
						duration: '14天',
						price: 18.50
					},
					{
						name: '氯雷他定片',
						specification: '10mg*6片/盒',
						manufacturer: '先灵葆雅制药有限公司',
						quantity: '2盒',
						usage: '口服',
						frequency: '每日1次',
						duration: '7天',
						price: 12.80
					},
					{
						name: '维生素E软胶囊',
						specification: '100mg*30粒/盒',
						manufacturer: '汤臣倍健股份有限公司',
						quantity: '1盒',
						usage: '口服',
						frequency: '每日2次',
						duration: '14天',
						price: 22.40
					}
				]
			},
			'014': {
				statusIcon: '⏳',
				statusText: '配药中',
				statusDesc: '药房正在为您配药，请耐心等待',
				deliveryStatus: '配药中',
				estimatedDelivery: '预计1小时内完成',
				medicineList: [
					{ name: '阿奇霉素片', specification: '250mg*6片/盒', manufacturer: '辉瑞制药有限公司', quantity: '1盒', usage: '口服', frequency: '每日1次', duration: '3天', price: 22.60 },
					{ name: '氨溴索口服溶液', specification: '100ml/瓶', manufacturer: '先声药业', quantity: '1瓶', usage: '口服', frequency: '每日3次', duration: '5天', price: 16.50 }
				]
			},
			'015': {
				statusIcon: '⏳',
				statusText: '配药中',
				statusDesc: '药房正在为您配药，请耐心等待',
				deliveryStatus: '配药中',
				estimatedDelivery: '预计1小时内完成',
				medicineList: [
					{ name: '萘敏维滴剂', specification: '10ml/瓶', manufacturer: '国药集团', quantity: '1瓶', usage: '含服', frequency: '每日3次', duration: '5天', price: 19.80 },
					{ name: '西地碘含片', specification: '24片/盒', manufacturer: '广州白云山', quantity: '1盒', usage: '含服', frequency: '每日3次', duration: '5天', price: 14.90 }
				]
			},
			'016': {
				statusIcon: '🚚',
				statusText: '配送中',
				statusDesc: '药品正在配送途中，请保持手机畅通',
				deliveryStatus: '配送中',
				estimatedDelivery: '预计今日下午送达',
				deliveryAddress: '湖南省长沙市岳麓区湘南院区附近',
				medicineList: [
					{ name: '阿司匹林肠溶片', specification: '100mg*30片/盒', manufacturer: '拜耳医药保健', quantity: '1盒', usage: '口服', frequency: '每日1次', duration: '30天', price: 23.50 },
					{ name: '瑞舒伐他汀片', specification: '10mg*7片/盒', manufacturer: '阿斯利康', quantity: '4盒', usage: '口服', frequency: '每日1次', duration: '28天', price: 35.80 }
				]
			},
			'017': {
				statusIcon: '⏳',
				statusText: '配药中',
				statusDesc: '药房正在为您配药，请耐心等待',
				deliveryStatus: '配药中',
				estimatedDelivery: '预计2小时内完成',
				medicineList: [
					{ name: '地奈德乳膏', specification: '15g/支', manufacturer: '和黄药业', quantity: '1支', usage: '外用', frequency: '每日2次', duration: '10天', price: 21.50 },
					{ name: '非索非那定片', specification: '60mg*10片/盒', manufacturer: '赛诺菲', quantity: '1盒', usage: '口服', frequency: '每日2次', duration: '7天', price: 29.90 }
				]
			}
		}

		return orderData[prescriptionId] || {
			statusIcon: '✓',
			statusText: '订单已提交',
			statusDesc: '您的订单已成功提交，请等待处理',
			deliveryStatus: '订单处理中',
			estimatedDelivery: '预计1-2个工作日',
			medicineList: []
		}
	},

	// 格式化日期时间
	formatDateTime(date) {
		const year = date.getFullYear()
		const month = String(date.getMonth() + 1).padStart(2, '0')
		const day = String(date.getDate()).padStart(2, '0')
		const hour = String(date.getHours()).padStart(2, '0')
		const minute = String(date.getMinutes()).padStart(2, '0')
		
		return `${year}-${month}-${day} ${hour}:${minute}`
	},

	// 下载订单
	downloadOrder() {
		console.log('下载订单')
		wx.showToast({
			title: '订单下载功能',
			icon: 'none',
			duration: 2000
		})
	},

	// 联系客服
	contactService() {
		console.log('联系客服')
		wx.showModal({
			title: '联系客服',
			content: '客服电话：400-123-4567\n工作时间：8:00-18:00',
			showCancel: false,
			confirmText: '知道了'
		})
	}
})
